#include "wave.h"
#include "wdtx_timer.h"
#include "wdtx_adc.h"
#include "wdtx_lcd5110.h"
#include <math.h>

struct waveStruct wave;

void displayInt32(uint32_t data ,uint8_t row)
{
	uint8_t i=13;
	while(data != 0)
	{
		LCD5110_printc(i--,row,data%10+'0');
		data/=10;
	}
	while(i-- >1)
	{
		LCD5110_printc(i--,row,' ');
	}
}

Wave getWave(void)
{
	uint8_t i;
	uint32_t tempRms = 0,max = 0;
	uint16_t *samplePoints = TIM3_getSamplePoints();
	
	wave.frequency = 1000000 / Tim2_GetCapValue();
	
	for(i = 0; i < SAMPLE_POINT_SIZE; i++)
	{
		if(samplePoints[i] > max)
		{
			max = samplePoints[i];
		}
		tempRms += samplePoints[i] * samplePoints[i];
	}
	
	//原因：总是有10mv的误差，加上10mv比较精确
	wave.vpp = 330 * (2 * max)/4096;
	tempRms = sqrt(tempRms / SAMPLE_POINT_SIZE);
	wave.rms = 330 * tempRms / 4096;
	displayInt32(tempRms,5);
	return &wave;
}


